package com.hjy.auth.web;

import cn.hutool.core.util.StrUtil;
import com.hjy.auth.feign.MemberFeignService;
import com.hjy.auth.feign.ThirdPartFeignService;
import com.hjy.auth.vo.LoginVo;
import com.hjy.auth.vo.RegisterVo;
import com.hjy.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    private ThirdPartFeignService thirdPartFeignService;
    @Autowired
    private MemberFeignService memberFeignService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping({"/","/login","/login.html"})
    public String login() {
        return "login";
    }

    @RequestMapping({"/register","/register.html"})
    public String goToRegister() {
        return "register";
    }


    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @RequestMapping("/user/sendCode")
    @ResponseBody
    public R sendCode(String phone) {
        thirdPartFeignService.sendCode(phone);
        return R.ok();
    }

    @PostMapping("/user/register")
    @ResponseBody
    public R register(@Validated @RequestBody RegisterVo registerVo , BindingResult bindingResult) {
        Map<String, String> collect = new HashMap<>();
        if (bindingResult.hasErrors()) {
            collect =
                    bindingResult.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error().put("error",collect);
        }
        // 注册逻辑
        // 1, 首先验证码效验
        String s = stringRedisTemplate.opsForValue().get("user:code:" + registerVo.getPhone());
        String code = "";
        if (StrUtil.isNotBlank(s)) {
             code = s.split("_")[0];
        }
        if (!registerVo.getCode().equals(code)) {
            collect.put("error","验证码不相同, 请重新获取验证码");
            return R.error().put("error",collect);
        }

        // 2. 发送请求给 gulimall-member 服务, 保存用户
        // 首先删除验证码
        stringRedisTemplate.delete("user:code:" + registerVo.getPhone());
        try {
            memberFeignService.register(registerVo);
        }catch (Exception e) {
            return R.error();
        }
        return R.ok();
    }

    @PostMapping("/user/login")
    @ResponseBody
    public R login(@RequestBody LoginVo loginVo) {
        try {
            memberFeignService.login(loginVo);
        }catch (Exception e) {
            return R.error();
        }
        return R.ok();
    }
}
